<template>
	<view class="wrap">
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="评论" autoBack @leftClick="toback">
			</u-navbar>
		</view>
		<view class="con-body">
			<view class="album">
				<view class="album__avatar">
					<u-avatar :src="item.avatar" shape="circle" mode="aspectFill">
					</u-avatar>
				</view>

				<view class="album__content">
					<view style="float: left;">
					
						<u-text :text="item.username" color="#3e3e3e" bold size="17"></u-text>
						<u-text margin="0 0 8rpx 0" color="#999999" size="14" :text="'来自 ' + item.from"></u-text>
					
					</view>
					<view v-if="item.status == 1" style="float: right; margin-right: 40rpx; ">
						<u-image width="100rpx" height="100rpx" src="https://resourse.cnlhjt.com/upload/20220825/ea67c8c95f615ffa9e2f4d3d81fc4316.png">
						</u-image>
					</view>

					<view style="margin-top: 20rpx; margin-bottom: 10rpx; clear: both;">
					<text style="color: #0B898E; font-size: 28rpx;">{{item.theme}}</text>
					<text style="color: #333333;font-size: 30rpx;margin-left: 10rpx;"> {{item.content}}</text>
						<u-album :urls="item.images" multipleSize="70" space="4"></u-album>
					</view>

					<view>
						<text style="color: #666666; font-size: 26rpx; float: left;">{{item.time}}</text>

						<view style="margin-right: 10rpx; float: right; display: inline;">
							<u-icon name="chat-fill" size="18" label-size="26rpx"
								color="#00aa00" :label="item.comment + '评论'"></u-icon>
						</view>
						<view style="margin-right: 10rpx; float: right; display: inline;">
							<u-icon name="heart" size="18" color="#ff0000" label-size="26rpx" :label="item.like + '赞'"
								@click="like(item.id, index)"></u-icon>
						</view>
						<view style="margin-right: 10rpx; float: right; display: inline;">
							<u-icon name="eye" size="18" color="#0055ff" label-size="26rpx" :label="item.see + '浏览'">
							</u-icon>
						</view>

					</view>
				</view>
			</view>
			<view>
				<u-line></u-line>
			</view>

			<u-toast ref="uToast"></u-toast>
			<view style="padding-left: 30rpx; padding-right: 30rpx; background-color: #FFFFFF;">
				<u-form-item style="width: 100%;">
					<u-input placeholder="请输入评论内容" v-model="commentValue">
						<template slot="suffix">
							<u-button @tap="commentDynamic" text="评论" type="primary" size="mini"></u-button>
						</template>
					</u-input>
				</u-form-item>
			</view>
		</view>
		<view class="fixed_bottom" v-if="istoreplay" style="border-top:1rpx solid #eee;">
			<view style="padding-left: 30rpx; padding-right: 30rpx; background-color: #FFFFFF;">
				<u-form-item style="width: 100%;">
					<u-input :placeholder="placeholder" :focus="focus" v-model="replycomment">
						<template slot="suffix">
							<u-button @tap="toreplycomment" text="发送" type="primary" size="mini"></u-button>
						</template>
					</u-input>
				</u-form-item>
			</view>
		</view>
		<view class="all-reply" style="padding-bottom:30rpx;">
			<view class="all-reply-top">全部评论（{{ item.comment }}）</view>
			<view class="item" v-for="(item, index) in commentList" :key="index">
				<view class="comment">
					<view class="top">
						<view class="left">
							<view class="heart-photo"><image :src="item.avatar" mode=""></image></view>
							<view class="user-info">
								<view class="name">{{ item.username }}</view>
								<view class="date">{{ item.time }}</view>
							</view>
						</view>
						<view class="right">
							<view class="user-info">
								<view class="date">{{ index + 1 }}楼</view>
							</view>
						</view>
					</view>
					<view class="content" style="margin-left: 75rpx;margin-top:10rpx;" @click="totworeplay(item)">{{ item.content }}</view>
					<view class="reply-box" style="padding-left:30rpx;">
						
						<view class="all-reply" @tap="toAllReply" v-if="item.allReply != 0">
							<view class="all-reply-top">全部回复（{{ item.allReply }}）</view>
							<view class="item" v-for="(item1, index1) in item.reply" :key="index1">
								<view class="comment">
									<view class="top">
										<view class="left">
											<view class="heart-photo"><image :src="item1.avatar" mode=""></image></view>
											<view class="user-info">
												<view class="name">{{ item1.username }}</view>
												<view class="date">{{ item1.time }}</view>
											</view>
										</view>
									</view>
									<view class="content" style="margin-left: 75rpx;margin-top:10rpx;" @click="totworeplay(item1)">{{ item1.content }}</view>
									<!-- 针对二层的回复评论 -->
									<view class="reply-box">
									
										<view class="all-reply" style="border:none;margin:0;" v-if="item1.allReply != 0">
											<!-- <view class="all-reply-top">全部回复（{{ item1.allReply }}）</view> -->
											<view class="item" v-for="(item2, index2) in item1.reply" :key="index2">
												<view class="comment" style="padding:30rpx 0 0;">
													<view class="top">
														<view class="left">
															<view class="heart-photo">
																<image :src="item2.avatar" mode=""></image>
															</view>
															<view class="user-info">
																<view class="name">{{ item2.username }}
																	<view class="dis_inl_blo"><u-icon name="play-right-fill" size="12" color="#999"></u-icon></view>
																	{{ item1.username }}
																</view>
																<view class="date">{{ item2.time }}</view>
															</view>
														</view>
													</view>
													<view class="content" style="margin-left: 75rpx;margin-top:10rpx;" >{{ item2.content }}</view>
												</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getDynamicDetail,
		getDynamicComment,
		commentDynamic,likeDynamic
	} from "@/service/shareService.js";
	export default {
		data() {
			return {
				commentList: [],
				item: {},
				commentValue: '',
				gid: 0,
				allReply:0,
				istoreplay:false,
				focus:false  ,//回复聚焦
				replyid:0,
				replycomment:'',
				placeholder:'请输入回复内容'
			};
		},
		onLoad(options) {
			this.gid = options.id;
			getDynamicDetail({
				gid: options.id
			}).then(this.getDynamicDetail);
			getDynamicComment({
				gid: options.id
			}).then(this.getComment);
		},
		methods: {
			getDynamicDetail(e) {
				if (e.code == 200) {
					this.item = e.result;
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			},
			toback(){
				let pages = getCurrentPages();
				let beforePage = pages[pages.length - 2];
				beforePage.$vm.reFresh = Math.random()
			},
			totworeplay(item){
				this.istoreplay=true;
				this.focus=true;
				this.replyid=item.id;
				this.placeholder='@'+item.username
			},
			toreplycomment(){
				commentDynamic({
					id: this.replyid,
					gid:this.gid,
					content: this.replycomment
				}).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'success'
						});
						this.commentList = res.result.data.data;
						this.allReply = res.result.data.total;
						this.replyid=0;
						this.istoreplay=false
					} else {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'warning'
						});
					}
				});
				
				this.replycomment = '';
			},
			// 点赞
			getLike(index) {
				if (index === 0 || index > 0) {
					this.commentList[index].isLike = !this.commentList[index].isLike;
					if (this.commentList[index].isLike == true) {
						this.commentList[index].likeNum++;
					} else {
						this.commentList[index].likeNum--;
					}
				} else {
					if (this.comment.isLike == true) {
						this.comment.isLike = !this.comment.isLike;
						this.comment.likeNum--;
					} else {
						this.comment.isLike = !this.comment.isLike;
						this.comment.likeNum++;
					}
				}
			},
			// 评论列表
			getComment(e) {
				if (e.code == 200) {
					this.commentList = e.result.data;
					this.allReply = e.result.total;
				}
			},
			commentDynamic() {
				commentDynamic({
					gid: this.gid,
					content: this.commentValue
				}).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'success'
						});
						this.commentList = res.result.data.data;
						
						this.allReply = res.result.data.total;
					} else {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'warning'
						});
					}
				});

				this.commentValue = '';
			},
			like(id, index) {
				likeDynamic({
					gid: id
				}).then(res => {
					if (res.code == 200) {
						this.releases[index].like += 1;
						this.$refs.uToast.show({
			
							type: 'success',
							title: '成功',
							message: res.msg
						})
					} else {
						this.$refs.uToast.show({
			
							type: 'error',
							title: '失败',
							message: res.msg
						})
					}
				});
			},
		}
	};
</script>

<style lang="scss" scoped>
	.fixed_bottom{
		position: fixed;
		bottom:0;
		left:0;
		width:100%;
		z-index:999;
	}
	.comment {
		padding: 30rpx;
		font-size: 32rpx;
		background-color: #ffffff;

		.top {
			display: flex;
			justify-content: space-between;
		}

		.left {
			display: flex;

			.heart-photo {
				image {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
					background-color: #f2f2f2;
				}
			}

			.user-info {
				margin-left: 10rpx;

				.name {
					color: #008987;
					font-size: 28rpx;
					margin-bottom: 4rpx;
				}

				.date {
					font-size: 20rpx;
					color: $u-light-color;
				}
			}
		}

		.right {
			display: flex;
			font-size: 20rpx;
			align-items: center;
			color: #9a9a9a;

			.like {
				margin-left: 6rpx;
			}

			.num {
				font-size: 26rpx;
				color: #9a9a9a;
			}
		}

		.highlight {
			color: #008987;

			.num {
				color: #008987;
			}
		}
	}

	.all-reply {
		margin-top: 10rpx;
		padding-top: 20rpx;
		background-color: #ffffff;

		.all-reply-top {
			margin-left: 20rpx;
			padding-left: 20rpx;
			border-left: solid 4rpx #008987;
			font-size: 30rpx;
			font-weight: bold;
		}

		.item {
			// border-bottom: solid 2rpx $u-border-color;
		}
		.bottom{
			background-color: #FFFFFF;
		}

		.reply {
			padding: 20rpx;
			background-color: rgb(242, 242, 242);
			border-radius: 12rpx;
			margin: 10rpx 0;

			.username {
				font-size: 24rpx;
				color: #7a7a7a;
			}
		}
	}

	.album {
		@include flex;
		padding: 20rpx;
		margin-top: 1px;
		align-items: flex-start;
		background-color: #FFFFFF;
		border-radius: 10rpx;


		&__avatar {
			padding: 5rpx;
			border-radius: 3rpx;
		}

		&__content {
			margin-left: 20rpx;
			flex: 1;
		}
	}
</style>
